package com.jw.server.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.ydx.protobuf.OTIpcDef;
import cn.ydx.protobuf.OTIpcDef.OTIpcList;

import com.jw.server.model.OperateArrive;
import com.jw.server.model.OperateDepart;
import com.jw.server.model.OperateLogin;
import com.jw.server.model.OperateLogout;
import com.jw.server.model.OperatePay;
import com.jw.server.service.OperateArriveRepository;
import com.jw.server.service.OperateDepartRepository;
import com.jw.server.service.OperateLoginRepository;
import com.jw.server.service.OperateLogoutRepository;
import com.jw.server.service.OperatePayRepository;
import com.jw.server.util.ByteUtil;

/**
 * 操作经营数据解析入库
 * 
 * @author xfm 2018年5月17日
 */
@RestController
@RequestMapping("/operate")
public class OperateDataController {

	@Autowired
	OperateArriveRepository operateArriveRepository;

	@Autowired
	OperateDepartRepository operateDepartRepository;

	@Autowired
	OperateLoginRepository operateLoginRepository;

	@Autowired
	OperateLogoutRepository operateLogoutRepository;

	@Autowired
	OperatePayRepository operatePayRepository;

	@PostMapping("/arrive")
	public String operateArrive(@RequestBody byte[] content) {
		byte[] b = null;
		// 解压数据
		b = ByteUtil.decompress(content);
		try {
			// 拿到第一层
			OTIpcList b1 = OTIpcDef.OTIpcList.parseFrom(b);
			ListIterator<OTIpcDef.OTIpc> iter = b1.getOtpicList()
					.listIterator();
			List<OperateArrive> basepay = null;
			while (iter.hasNext()) {
				OTIpcDef.OTIpc o = iter.next();
				List<OTIpcDef.OperateArrive> li = o.getOperateArriveList();
				if (li != null && li.size() > 0) {
					Iterator<OTIpcDef.OperateArrive> l = li.iterator();
					basepay = new ArrayList<OperateArrive>();
					while (l.hasNext()) {
						OTIpcDef.OperateArrive base = l.next();
						basepay.add(OperateArrive.convertPojo(base));
					}
				}
			}
			// 将数据存入数据库 basepay
			System.out.println("收operateArrive表的数据条数："+basepay.size());
			operateArriveRepository.save(basepay);

		} catch (Exception e) {
			e.printStackTrace();
			return "400";
		}
		return "200";
	}

	@PostMapping("/depart")
	public String operateDepart(@RequestBody byte[] content) {
		byte[] b = null;
		// 解压数据
		b = ByteUtil.decompress(content);
		try {
			// 拿到第一层
			OTIpcList b1 = OTIpcDef.OTIpcList.parseFrom(b);
			ListIterator<OTIpcDef.OTIpc> iter = b1.getOtpicList()
					.listIterator();
			List<OperateDepart> basepay = null;
			while (iter.hasNext()) {
				OTIpcDef.OTIpc o = iter.next();
				List<OTIpcDef.OperateDepart> li = o.getOperateDepartList();
				if (li != null && li.size() > 0) {
					Iterator<OTIpcDef.OperateDepart> l = li.iterator();
					basepay = new ArrayList<OperateDepart>();
					while (l.hasNext()) {
						OTIpcDef.OperateDepart base = l.next();
						basepay.add(OperateDepart.convertPojo(base));
					}
				}
			}
			// 将数据存入数据库 basepay
			System.out.println("收operateDepart表的数据条数："+basepay.size());
			operateDepartRepository.save(basepay);

		} catch (Exception e) {
			e.printStackTrace();
			return "400";	
		}
		return "200";
	}

	@PostMapping("/login")
	public String operateLogin(@RequestBody byte[] content) {
		byte[] b = null;
		// 解压数据
		b = ByteUtil.decompress(content);
		try {
			// 拿到第一层
			OTIpcList b1 = OTIpcDef.OTIpcList.parseFrom(b);
			ListIterator<OTIpcDef.OTIpc> iter = b1.getOtpicList()
					.listIterator();
			List<OperateLogin> basepay = null;
			while (iter.hasNext()) {
				OTIpcDef.OTIpc o = iter.next();
				List<OTIpcDef.OperateLogin> li = o.getOperateLoginList();
				if (li != null && li.size() > 0) {
					Iterator<OTIpcDef.OperateLogin> l = li.iterator();
					basepay = new ArrayList<OperateLogin>();
					while (l.hasNext()) {
						OTIpcDef.OperateLogin base = l.next();
						basepay.add(OperateLogin.convertPojo(base));
					}
				}
			}
			// 将数据存入数据库 basepay
			System.out.println("收operateLogin表的数据条数："+basepay.size());
			operateLoginRepository.save(basepay);

		} catch (Exception e) {
			e.printStackTrace();
			return "400";
		}
		return "200";
	}

	@PostMapping("/logout")
	public String operateLogout(@RequestBody byte[] content) {
		byte[] b = null;
		// 解压数据
		b = ByteUtil.decompress(content);
		try {
			// 拿到第一层
			OTIpcList b1 = OTIpcDef.OTIpcList.parseFrom(b);
			ListIterator<OTIpcDef.OTIpc> iter = b1.getOtpicList()
					.listIterator();
			List<OperateLogout> basepay = null;
			while (iter.hasNext()) {
				OTIpcDef.OTIpc o = iter.next();
				List<OTIpcDef.OperateLogout> li = o.getOperateLogoutList();
				if (li != null && li.size() > 0) {
					Iterator<OTIpcDef.OperateLogout> l = li.iterator();
					basepay = new ArrayList<OperateLogout>();
					while (l.hasNext()) {
						OTIpcDef.OperateLogout base = l.next();
						basepay.add(OperateLogout.convertPojo(base));
					}
				}
			}
			// 将数据存入数据库 basepay
			System.out.println("收operateLogout表的数据条数："+basepay.size());
			operateLogoutRepository.save(basepay);

		} catch (Exception e) {
			e.printStackTrace();
			return "400";
		}
		return "200";
	}

	@PostMapping("/pay")
	public String operatePay(@RequestBody byte[] content) {
		byte[] b = null;
		// 解压数据
		b = ByteUtil.decompress(content);
		try {
			// 拿到第一层
			OTIpcList b1 = OTIpcDef.OTIpcList.parseFrom(b);
			ListIterator<OTIpcDef.OTIpc> iter = b1.getOtpicList()
					.listIterator();
			List<OperatePay> basepay = null;
			while (iter.hasNext()) {
				OTIpcDef.OTIpc o = iter.next();
				List<OTIpcDef.OperatePay> li = o.getOperatePayList();
				if (li != null && li.size() > 0) {
					Iterator<OTIpcDef.OperatePay> l = li.iterator();
					basepay = new ArrayList<OperatePay>();
					while (l.hasNext()) {
						OTIpcDef.OperatePay base = l.next();
						basepay.add(OperatePay.convertPojo(base));
					}
				}
			}
			// 将数据存入数据库 basepay
			System.out.println("收operatePay表的数据条数："+basepay.size());
			operatePayRepository.save(basepay);

		} catch (Exception e) {
			e.printStackTrace();
			return "400";
		}
		return "200";
	}

}
